/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_admin;

import battleship_client.Controller;
import battleship_client.FilterManager;
import battleship_client.screens.AdminScreen;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author omar
 */
public class AdminClientThread extends Thread {

    FilterManager fm = null;
    public boolean bandera = true;
    ArrayList<String> players = new ArrayList<String>();
    public int numJugadores = 0;
    public boolean notifico = false;
    public AdminScreen screen = null;
    public int activeGames = 0;

    public AdminClientThread(FilterManager fm) {
        this.fm = fm;
        screen = new AdminScreen();

    }

    public void run() {
        String s = "";
        screen.setVisible(true);
        while (bandera) {
            try {
                s = fm.readLine();
                System.out.println(s);
                if (s.equalsIgnoreCase("Player-Change")) {
                    while (!(s = fm.readLine()).equalsIgnoreCase("END")) {
                        System.out.println(s);
                        if (!players.contains(s)) {
                            players.add(s);
                        }
                    }
                    notifico = true;
                } else if (s.equalsIgnoreCase("Player-Gone")) {
                    while (!(s = fm.readLine()).equalsIgnoreCase("END")) {
                        players.remove(s);
                    }
                    notifico = true;
                } else if (s.equalsIgnoreCase("ActiveG-Change")) {
                    activeGames = Integer.parseInt(fm.readLine());
                    notifico = true;
                }
                if (notifico) {
                    //pues notifico a la ventana...
                    System.out.println("Players = " + players.toString());
                    screen.changeText(players.toString());

                    screen.changeTextActiveG(activeGames + " active games.");

                    notifico = false;
                }
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Logger.getLogger(AdminClientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
